package com.sjq.demo.consumer;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Queue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.sjq.common.mq.base.AbstractJMSConsumer;
import com.sjq.common.mq.base.ConsumException;

@Component
public class DemoJMSConsumer extends AbstractJMSConsumer {

	private static Logger logger = LoggerFactory
			.getLogger(DemoJMSConsumer.class);

	//////////////!!!!!!!!!!!!名字	
	private static final String DEMO_QUEUE_NAME= "sjq_msg_queue";	
	
	/**
	 * 使用Queue更加适合topic 原因： Sender发送消息到目标Queue，receiver可以异步接收这个Queue上的消息。
	 * Queue上的消息如果暂时没有receiver来取，也不会丢失。
	 * 
	 */
	@Override
	public Class<? extends Destination> initialize() {
		super.specifyDestinationName(DEMO_QUEUE_NAME);
		System.out.println("创建具体消费者"+DEMO_QUEUE_NAME+"成功！");
		return Queue.class;
	}

	@Override
	public int specifyMaximumRedeliveries() {
		return 1;
	}

	@Override
	public void preDestroy() {
		logger.info("消费者销毁，线程:"
				+ Thread.currentThread().getName());
	}

	@Override
	public boolean doConsume(Message message) throws ConsumException,
			JMSException {
		ObjectMessage objMsg = (ObjectMessage) message;
		return doConsumeByReceiver(objMsg);
	}

	private boolean doConsumeByReceiver(ObjectMessage message) {
		//业务方法，消费掉需要返回true
		System.err.println("*********收到到队列"+DEMO_QUEUE_NAME+"的消息为：");
		System.out.println(message.toString());
		
		try {
			System.out.println("***********具体消息是"+message.getObject().toString());
		} catch (JMSException e) {
			e.printStackTrace();
		}
		return true;
	}

}
